﻿using System.Collections.Generic;
using System.Globalization;
using System.Resources;
using System.Windows.Forms;
using TSites.GUI.Languages;
using System;
using TSites.GUI.Enums;
using VNMessageBox;

namespace TSites.GUI
{
	public partial class frmExcelImport : Form
	{
		public frmExcelImport()
		{
			InitializeComponent();
		}

		#region Methods

		private void ApplyResource()
		{
			CultureInfo culture = CultureInfoFactory.GetInstance();
			ResourceManager rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmExcelImport).Assembly);

			lblFilePath.Text = rm.GetString("FilePath", culture) + ":";
			lblWorksheets.Text = rm.GetString("WorkSheetName", culture) + ":";

			btnOK.Text = rm.GetString("OK", culture);
			btnCancel.Text = rm.GetString("Cancel", culture);

			this.Text = rm.GetString("frmExcelImport", culture);
		}

		private void OpenFileDialog()
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Multiselect = false;
			dlg.Filter = "Excel Files|*.xls|Excel 2007 Files|*.xlsx|All Files|*.*";
			if (dlg.ShowDialog() == DialogResult.OK)
			{
				txtFilePath.Text = dlg.FileName;

				List<string> worksheets = null;
				try
				{
					worksheets = CompanySingleton.Instance.GetWorkSheets(dlg.FileName);
					if (worksheets != null && worksheets.Count > 0)
					{
						cbxWorksheet.Enabled = btnOK.Enabled = true;
						cbxWorksheet.DataSource = worksheets;
						cbxWorksheet.SelectedIndex = 0;
					}
				}
				catch (Exception ex)
				{
					LANGUAGE language = ConfigurationSingleton.Instance.Language;
					CultureInfo culture = CultureInfoFactory.GetInstance();
					ResourceManager rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmExcelImport).Assembly);

					string title = rm.GetString("Error", culture);
					if (language == LANGUAGE.Vietnames)
					{
						CMessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
					else
					{
						MessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}
		
		#endregion

		#region Events
		
		private void frmExcelImport_Load(object sender, System.EventArgs e)
		{
			ApplyResource();
			cbxWorksheet.Enabled = btnOK.Enabled = false;
		}

		private void btnBrowse_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog();
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			string filePath = txtFilePath.Text;
			string worksheet = cbxWorksheet.Text;

			int rows = -1;
			try
			{
				rows = CompanySingleton.Instance.ImportExcel(filePath, worksheet);
			}
			catch (Exception ex)
			{
				LANGUAGE language = ConfigurationSingleton.Instance.Language;
				CultureInfo culture = CultureInfoFactory.GetInstance();
				ResourceManager rm = new ResourceManager(CultureInfoFactory.BASEDNAME, typeof(frmExcelImport).Assembly);

				string title = rm.GetString("Error", culture);
				if (language == LANGUAGE.Vietnames)
				{
					CMessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				else
				{
					MessageBox.Show(ex.StackTrace, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}

			this.DialogResult = rows > 0 ? DialogResult.OK : DialogResult.Cancel;
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		#endregion
	}
}
